<?php
/**
 * This file is part of the Jindai.
 * @copyright Copyright (c) 2019 All Rights Reserved.
 * @author jindai <jindai0305@gmail.com>
 */

namespace App\Models;

use App\Models\traits\PublishTrait;
use App\Models\traits\SoftDeleteTrait;

/**
 * Class Song
 * @package App\Models
 *
 * @property integer $id
 * @property string $name
 * @property string $song_id
 * @property array $song_images
 * @property string $singer_id
 * @property string $singer
 * @property array $tags
 * @property string $album_name
 * @property string $album_id
 * @property array $album_images
 * @property string $content_id
 * @property string $copyright_id
 * @property string $invalidate
 * @property string $lrc_url
 * @property string $mrc_url
 * @property string $top_quality
 * @property string $top_url
 * @property string $rate_formats
 * @property integer $status
 * @property integer $created_at
 * @property integer $deleted_at
 * @property integer $flag
 */
class Song extends BaseModel
{
    use PublishTrait, SoftDeleteTrait;

    protected $fillable = [
        'name', 'song_id', 'song_images', 'singer_id', 'singer', 'tags', 'album_name', 'album_id', 'album_images',
        'content_id', 'copyright_id', 'invalidate', 'lrc_url', 'mrc_url', 'top_quality', 'top_url',
        'rate_formats', 'status', 'flag'
    ];

    const UPDATED_AT = null;

    /**
     * @inheritdoc
     */
    public function getTable()
    {
        return 'songs';
    }

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function songMenu()
    {
        return $this->belongsToMany(SongMenu::class, 'songables', 'song_id', 'menu_id');
    }

    /**
     * @inheritdoc
     */
    protected function appendCasts(): array
    {
        return [
            'tags' => 'array',
            'song_images' => 'array',
            'album_images' => 'array',
            'rate_formats' => 'array',
        ];
    }

    /**
     * 获取歌曲链接
     * @return string
     */
    public function getSongUrl()
    {
        $url = 'http://app.pd.nf.migu.cn/MIGUM2.0/v1.0/content/sub/listenSong.do?toneFlag=%s&netType=00&userId=15548614588710179085069&ua=Android_migu&version=5.1&copyrightId=0&contentId=%d&resourceType=%s&channel=0';
        if ($this->top_quality == "SQ") {
            return sprintf($url, 'SQ', $this->content_id, 'E');
        }
        return sprintf($url, 'HQ', $this->content_id, 2);
    }

    /**
     * 获取歌词地址
     *
     * @return bool|string
     */
    public function getLrcUrl()
    {
        if ($this->lrc_url) {
            return $this->lrc_url;
        }
        if ($this->mrc_url) {
            return $this->mrc_url;
        }
        return false;
    }
}
